package 力扣._00周赛._146双周赛.问题3;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

class Solution {
    public boolean checkValidCuts(int n, int[][] rectangles) {
        // baoli
        HashSet<Integer> row_list = new HashSet<>();
        HashSet<Integer> col_list = new HashSet<>();
        HashSet<Integer> t = new HashSet<>();

        //row:横线切割
        for (int[] rectangle : rectangles) {
            row_list.add(rectangle[1]);
            row_list.add(rectangle[3]);
        }
        // 删除在矩形内部的切割线
        for (Integer r : row_list) {
            for (int[] rectangle : rectangles) {
                if (r > rectangle[1] && r < rectangle[3]) {
                    t.add(r);
                    break;
                }
            }
        }
        for (Integer r : t) {
            row_list.remove(r);
        }


        System.out.println(row_list);

        if(row_list.size() >= 4){
            return true;
        }
        //col:竖线切割
        for (int[] rectangle : rectangles) {
            col_list.add(rectangle[0]);
            col_list.add(rectangle[2]);
        }
        // 删除在矩形内部的切割线
        t.clear();
        for (Integer c : col_list) {
            for (int[] rectangle : rectangles) {
                if (c > rectangle[0] && c < rectangle[2]) {
                    t.add(c);
                    break;
                }
            }
        }
        for (Integer c : t) {
            col_list.remove(c);
        }
        System.out.println(col_list);
        return col_list.size() >= 4;

    }

    public static void main(String[] args) {
        // 5
        //[[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]]©leetcode
//        int n = 5;
//        int[][] rectangles = new int[][]{{1, 0, 5, 2}, {0, 2, 2, 4}, {3, 2, 5, 3}, {0, 4, 4, 5}};
//        System.out.println(new Solution().checkValidCuts(n, rectangles));

        // 4
        // [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]]©leetcode
        int n = 4;
        int[][] rectangles = new int[][]{{0,2,2,4},{1,0,3,2},{2,2,3,4},{3,0,4,2},{3,2,4,4}};
        System.out.println(new Solution().checkValidCuts(n, rectangles));
    }
}